/**
 * Project Name:BGI_DX
 * File Name:RoleAction.java
 * Package Name:com.bgi.dx.module.sys.role.action
 * Date:2014-10-24下午01:26:53
 * Copyright (c) 2014, BGI_DX All Rights Reserved.
 *
*/

package com.bgi.uims.module.sys.role.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bgi.uims.common.action.BaseAction;
import com.bgi.uims.common.dto.PageVo;
import com.bgi.uims.module.sys.operatorlog.dto.SysOperatorLog;
import com.bgi.uims.module.sys.portal.dto.Portal;
import com.bgi.uims.module.sys.portal.service.PortalSysService;
import com.bgi.uims.module.sys.role.dto.RolePortal;
import com.bgi.uims.module.sys.role.dto.SysRoles;
import com.bgi.uims.module.sys.role.service.SysRolesService;
import com.bgi.uims.module.sys.user.dto.User;
import com.bgi.uims.pub.utils.Constant;
import com.bgi.uims.pub.utils.PublicMethod;


/**
 * ClassName:RoleAction <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2014-10-24 下午01:26:53 <br/>
 * @author   fukun
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
@Controller
@Scope("prototype")
public class SysRolesAction extends BaseAction{

	
/**
	 * serialVersionUID:TODO(用一句话描述这个变量表示什么).
	 * @since JDK 1.7
	 */
	private static final long serialVersionUID = 1L;
	
	
	//	List<TreeJson> list = TreeJson.formatTree(dao.queryATree()) ;
	private PageVo pageVo;
	private SysRoles role;
	private RolePortal rolePortal;
	private SysOperatorLog sysOperatorLog;

	@Resource
	private SysRolesService roleService;
	@Resource
	private PortalSysService portalSysService;
	
	public SysRoles getRole() {
		return role;
	}

	public void setRole(SysRoles role) {
		this.role = role;
	}
	
	
	public PageVo getPageVo() {
		return pageVo;
	}

	public void setPageVo(PageVo pageVo) {
		this.pageVo = pageVo;
	}
	
	public SysOperatorLog getSysOperatorLog() {
		return sysOperatorLog;
	}

	public void setSysOperatorLog(SysOperatorLog sysOperatorLog) {
		this.sysOperatorLog = sysOperatorLog;
	}

	/**  
	 * rolePortal  
	 * @return  the rolePortal  
	 */
	
	public RolePortal getRolePortal() {
		return rolePortal;
	}

	/**  
	 * @param rolePortal the rolePortal to set  
	 */
	
	public void setRolePortal(RolePortal rolePortal) {
		this.rolePortal = rolePortal;
	}

	/**
	 * 
	 * @Description：  查询角色信息
	 * @author: WEN
	 * @CreateDate:2015年3月31日 下午1:39:15
	 * @version: V1.0
	 */
	public String findRole()
	{
		if(role==null)
		{
			role=new SysRoles();
		}
		role.getPage(this);
		pageVo=roleService.findSysRoles(role);
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT,Constant.ROLEMODULES,role, null));
		pageVo.setSuccess(true);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  id号查询角色
	 * @author: WEN
	 * @CreateDate:2015年3月31日 下午1:38:33
	 * @version: V1.0
	 */
	public String findRoleById()
	{
		pageVo=new PageVo();
		pageVo.setObject(roleService.findRoleById(role));
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT, Constant.ROLEMODULES,role, null));
		pageVo.setSuccess(true);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  保存/修改角色信息
	 * @author: WEN
	 * @CreateDate:2015年3月31日 下午1:37:36
	 * @version: V1.0
	 */
	public String saveRole()
	{
		pageVo=new PageVo();
		if(role==null){
			pageVo.setSuccess(false);
			pageVo.setMsg("角色名不允许为空!");
		}
		else
		{
			//获取登录的session
			User user=(User) session.get(Constant.LOGINUSER);
			if(role.getRolesId()!=null&&!"".equals(role.getRolesId())){
				SysRoles oldRole = roleService.findRoleById(role);
				role.setLastModifiedUser(user.getRealName());
				pageVo=roleService.saveRole(role);
				session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.UPDATE,Constant.ROLEMODULES , oldRole, role));
			} else {
				role.setCreateUser(user.getRealName());
				pageVo=roleService.saveRole(role);
				session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.INSERT, Constant.ROLEMODULES ,role, null));
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  ID删除角色信息
	 * @author: WEN
	 * @CreateDate:2015年3月31日 下午1:34:45
	 * @version: V1.0
	 */
	public String deleteRole(){
		pageVo=new PageVo();
		SysRoles oldRole = roleService.findRoleById(role);
		boolean f=  roleService.deleteSysRoles(role);
		if(f) session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.DELETE,Constant.ROLEMODULES , oldRole, null));
		pageVo.setSuccess(f);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  获取当前角色所拥有的面板
	 * @author: wuyaohong
	 * @CreateDate:2015年3月30日 下午3:34:31
	 * @version: V1.0
	 */
	public String getRolePortalInfo(){
		pageVo = new PageVo();
		String html = roleService.getRolePortalInfo(role);
		pageVo.setSuccess(true);
		pageVo.setObject(html);
		session.put(Constant.OPERATIONMSG, PublicMethod.operationMsg(Constant.SELECT, Constant.ROLEMODULES,role, null));
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  保存角色面板授权
	 * @author: wuyaohong
	 * @CreateDate:2015年3月30日 下午3:32:14
	 * @version: V1.0
	 */
	public String saveRolePortal(){
		pageVo = new PageVo();
		List<RolePortal> oldlist = new ArrayList<RolePortal>();
		if(rolePortal.getPortalId().length() > 0){
			role = new SysRoles();
			role.setRolesId(rolePortal.getRoleId());
			oldlist = roleService.getPortalByRoleId(role);
			String[] portalIds = rolePortal.getPortalId().split(",");
			List<String> oldIds = getIdList(oldlist);
			List<String> newIds = Arrays.asList(portalIds);
			StringBuffer message = new StringBuffer();
			message.append("更新内容为：");
			if(newIds.size() > 0){
				if(oldIds.size() > 0){
					for(int i = 0; i < newIds.size(); i++){
						for(RolePortal rp : oldlist){
							if(rp.getPortalId().equals(newIds.get(i))){
								continue;
							}else{
								if(!newIds.contains(rp.getPortalId())){
									Portal portal = new Portal();
									portal.setPortalId(rp.getPortalId());
									Portal p = portalSysService.findPortalById(portal);
									if(message.indexOf(p.getPortalName()) < 0){
										message.append("删除了面板'"+p.getPortalName()+"'，");
									}
								}
								if(!oldIds.contains(newIds.get(i))){
									Portal portal = new Portal();
									portal.setPortalId(newIds.get(i));
									Portal p = portalSysService.findPortalById(portal);
									if(message.indexOf(p.getPortalName()) < 0){
										message.append("新关联了面板'"+p.getPortalName()+"'，");
									}
								}
								continue;
							}
						}
					}
				}else{
					for(int i = 0; i < newIds.size(); i++){
						Portal portal = new Portal();
						portal.setPortalId(newIds.get(i));
						Portal p = portalSysService.findPortalById(portal);
						message.append("新关联了面板'"+p.getPortalName()+"'，");
					}
				}
			}else{
				message.append("删除了面板");
				for(RolePortal rp : oldlist){
					Portal portal = new Portal();
					portal.setPortalId(rp.getPortalId());
					Portal p = portalSysService.findPortalById(portal);
					message.append("'"+p.getPortalName()+"'，");
				}
			}
			String[] msgStr=new String[5];
			msgStr[0] = "sys_roles";
			msgStr[1] = rolePortal.getRoleId();
			msgStr[2] = "3";
			msgStr[3] = message.toString();
			//不需要存太长
			if(msgStr[3].length()>512)
			{
				msgStr[3]=msgStr[3].substring(0, 508)+"....";
			}
			msgStr[4] = Constant.ROLEMODULES;
			session.put(Constant.OPERATIONMSG, msgStr);
		}
		boolean result = roleService.saveRolePortal(rolePortal);
		if(result){
			pageVo.setSuccess(true);
		}else{
			pageVo.setSuccess(false);
			pageVo.setMsg("保存失败！");
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Description：  转换成Id 集合
	 * @author: wuyaohong
	 * @CreateDate:2015年3月30日 下午3:31:06
	 * @version: V1.0
	 */
	public List<String> getIdList(List<RolePortal> rolePortals){
		List<String> list = new ArrayList<String>();
		for(RolePortal rp : rolePortals){
			list.add(rp.getPortalId());
		}
		return list;
	}
}

